package utils

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"log"
	"time"
)
var(
	Db  *gorm.DB
)
func init() {
	var err error
	mysql_conn := fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		"root",
		"lmh123",
		"127.0.0.1",
		3306,
		"gin_jt")
	Db,err = gorm.Open("mysql",mysql_conn)
	//Db,err = gorm.Open("mysql","root:lmh123@(localhost:3306)/gin_jt?charset=utf8&parseTime=True&loc=Local")
	//Db,err = gorm.Open("mysql","root:lmh123@/gin_jt?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("初始化数据库成功......")
	//Db.LogMode(false)  //是否开启log
	//Db.AutoMigrate( &models.Article{},&models.G_count{}) //自动迁移  跟py一样

	//DB pool
	Db.DB().SetMaxIdleConns(5)
	Db.DB().SetMaxOpenConns(100)
	Db.DB().SetConnMaxLifetime(time.Hour)

}

